﻿using CaseManagerLibrary.Encrypt;
using CaseManagerLibrary.Licence;
using PSDLib.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PSDManager.SystemSettings
{
    public partial class FrmPSDDevice : Form
    {
        public List<dl_server_config> ServerList { get; set; }
        public dl_download_device PSD { get; set; }
        public FrmPSDDevice()
        {
            InitializeComponent();

            storagePanel1.SetStorageType(0);
        }

        //public void SetServer(List<dl_server_config> servers)
        //{
        //    ServerList = servers;
        //    this.cmbServer.Items.Clear();
        //    foreach (var s in servers)
        //    {
        //        cmbServer.Items.Add(s.storage_server_type + " - " + s.storage_server_ip);
        //    }
        //    cmbDeviceVersion.SelectedIndex = -1;
        //}

        public void SetPSD(dl_download_device psd)
        {
            PSD = psd;

            if (psd != null)
            {
                

                this.txtID.Text = psd.id;
                this.txtDeviceName.Text = psd.device_name;
                this.txtPwd.Text = psd.device_pwd;
                this.txtConfirmPwd.Text = psd.device_pwd;
                this.cmbDeviceVersion.SelectedItem = psd.device_version;
                this.txtDeviceIP.Text = psd.device_ip;
                this.txtDevicePort.Text = psd.device_port.ToString();
                //this.cmbServer.SelectedIndex = ServerList.FindIndex(t => t.id == psd.dl_server_id);
                this.cmbProcess.SelectedIndex = psd.max_download_process - 1;
                this.cmbStorageType.SelectedIndex = psd.storage_type;
                this.txtStoragePercent.Text = psd.storage_available_percent.ToString();
                this.txtDataPath.Text = psd.data_path;
                this.txtLogPath.Text = psd.log_path;

                var licence = LicenceManager.GetLicenceContent(psd.device_key, psd.id);

                var code = LicenceManager.Check(licence, psd.id, LicenceDaysFrom.SysTime);

                if (code == LicenceCheckCode.Success)
                {
                    lblState.Text = "已注册";
                    lblState.ForeColor = Color.Green;
                    lblState.Tag = 1;
                }
                else if (code == LicenceCheckCode.Expried)
                {
                    lblState.Text = "授权已过期，请点击此处重新授权";
                    lblState.ForeColor = Color.Red;
                    lblState.Tag = 2;
                }
                else
                {
                    lblState.Text = "尚未注册，请点击此处注册";
                    lblState.ForeColor = Color.Red;
                    lblState.Tag = 0;
                }

                storagePanel1.SetStorages(psd.StorageList);
                storagePanel1.SetStorageType(psd.storage_type);
            }
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            if (PSD != null)
            {
                if (txtPwd.Text.Trim() != txtConfirmPwd.Text.Trim())
                {
                    MessageBox.Show("设置的设备密码不一致！");
                    return;
                }
                PSD.id = this.txtID.Text.Trim();
                PSD.device_name = this.txtDeviceName.Text.Trim();
                PSD.device_version = this.cmbDeviceVersion.SelectedItem.ToString();
                PSD.device_ip = this.txtDeviceIP.Text.Trim();
                PSD.device_port = Convert.ToInt32(this.txtDevicePort.Text.Trim());
                //if (ServerList != null && this.cmbServer.SelectedIndex >= 0)
                //{
                //    PSD.dl_server_id = ServerList[this.cmbServer.SelectedIndex].id;
                //}
                PSD.max_download_process = this.cmbProcess.SelectedIndex + 1;
                PSD.storage_type = cmbStorageType.SelectedIndex;
                PSD.storage_available_percent = Convert.ToDouble(this.txtStoragePercent.Text.Trim());
                PSD.data_path = this.txtDataPath.Text.Trim();
                PSD.log_path = this.txtLogPath.Text.Trim();
                PSD.state = Convert.ToInt32(lblState.Tag);
                if (PSD.device_pwd != txtPwd.Text.Trim())
                {
                    byte[] b = System.Text.Encoding.UTF8.GetBytes(txtPwd.Text.Trim());
                    PSD.device_pwd = Convert.ToBase64String(b);
                }

                var curr_st = PSD.StorageList.Where(t => t.storage_type == 0 && t.state == 1).FirstOrDefault();
                if (curr_st == null)
                {
                    foreach (var st in PSD.StorageList)
                    {
                        if (st.storage_type == 0 && st.state != 2)
                        {
                            st.state = 1;
                            curr_st = st;
                            break;
                        }
                    }
                }
                if (curr_st != null)
                {
                    PSD.current_storage = curr_st.storage_driver;
                }
                else
                {
                    PSD.current_storage = "";
                }
            }

            DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void lblState_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(lblState.Tag) != 1)
            {
                var ofg = new OpenFileDialog();
                ofg.Filter = "*.pal|*.pal";

                if (ofg.ShowDialog() == DialogResult.OK)
                {
                    var licence = LicenceManager.GetLicenceContentFromFile(ofg.FileName, this.txtID.Text);

                    if (licence != null)
                    {
                        var code = LicenceManager.Check(licence, this.txtID.Text, LicenceDaysFrom.Ignore);

                        if (code == LicenceCheckCode.Success)
                        {
                            PSD.device_key = System.IO.File.ReadAllText(ofg.FileName);
                            PSD.state = 1;
                            this.lblState.Text = "已注册";
                            this.lblState.ForeColor = Color.Green;
                            lblState.Tag = 1;
                        }
                    }
                }
            }
        }

        private void cmbStorageType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbStorageType.SelectedIndex >= 0)
            {
                storagePanel1.SetStorageType(cmbStorageType.SelectedIndex);
            }
        }

        private void btnResetStorage_Click(object sender, EventArgs e)
        {
            storagePanel1.ResetStorage(PSD);
        }
    }
}
